package com.nsgf.library.ui.page.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.databinding.FragmentNsgfManagerBinding;
import com.nsgf.library.ui.adapter.XFragmentAdapter;
import com.nsgf.library.ui.page.AlarmEventCompletedActivity;
import com.nsgf.library.ui.page.AlarmEventProcessActivity;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.ui.page.fragment.ManagerPageFragment;
import com.nsgf.library.utils.PublicMethodUtils;

import java.util.ArrayList;
import java.util.List;

public class ManagerFragment extends BaseFragment {

    private MineFragmentStates mStates;

    private FragmentNsgfManagerBinding mBinding;
    private List<Fragment> fragmentList = new ArrayList<>();

    private String[] mPageTitleList;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(MineFragmentStates.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fragment_nsgf_manager, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            // Fragment 通过 show() 变为可见, 刷新数据
            ((ManagerPageFragment) fragmentList.get(0)).onRefresh();
            ((ManagerPageFragment) fragmentList.get(1)).onRefresh();
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding = (FragmentNsgfManagerBinding) getBinding();
        mBinding.viewPager.setOffscreenPageLimit(2);
        fragmentList.add(ManagerPageFragment.newInstance(0));
        fragmentList.add(ManagerPageFragment.newInstance(1));
        mPageTitleList = new String[]{"待办", "已办"};
        XFragmentAdapter adapter = new XFragmentAdapter(getActivity(), getChildFragmentManager(), fragmentList, mPageTitleList);
        mBinding.viewPager.setAdapter(adapter);

        PublicMethodUtils.initMagicIndicator(mBinding.pageIndicatorView, mBinding.viewPager, mPageTitleList, getActivity());

    }

    public static class MineFragmentStates extends StateHolder {

        public final State<Boolean> loadingVisible = new State<>(false);
        public final State<Integer> searchVisible = new State<>(View.GONE);
        public final State<String> search = new State<>("");

    }


    public class ClickProxy {

        public void slideSearchBox() {

            mStates.searchVisible.set(View.VISIBLE);
        }

        public void search() {
            ((ManagerPageFragment) fragmentList.get(0)).search(mStates.search.get());
            ((ManagerPageFragment) fragmentList.get(1)).search(mStates.search.get());
        }
    }
}
